home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4895 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  44 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!news1!news
  3. From: rclark@iquest.net (Robert B. Clark)
  4. Subject: Re: ???Recursive Function needed for string printing
  5. X-Nntp-Posting-Host: ind-009-237-105.iquest.net
  6. Message-ID: <31190090.624002@news.iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Organization: IQuest Internet, Inc.
  9. X-Newsreader: Forte Agent .99d/16.182
  10. References: <4f44eo$74u@upsidedown.MTS.Net>
  11. Date: Wed, 7 Feb 1996 19:46:30 GMT
  12.  
  13. On Mon, 05 Feb 1996 07:26:19 GMT, bwilliam@MTS.Net (George) wrote:
  14.  
  15. >How do I write a recursive function to print a string  using only
  16. >printf and %c to print it out.
  17.  
  18. Something like this...
  19.  
  20. /* rstring.c - Recursively print character array string */
  21. /* R Clark <rclark@iquest.net> 7 Feb 1996 */
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. void recurseString(char *s)
  27. /* Recursively prints head character until head char==\0 */
  28. {
  29.    if (*s!='\0') {         /* Exit condition */
  30.       putchar(*s);           /* Or printf("%c",*s) if you prefer */
  31.       recurseString(++s);  /* Increment char pointer, recurse */
  32.    }
  33. }
  34.  
  35. int main(void)
  36. {
  37.    puts("\nRecursive string print:");
  38.    recurseString("This string will be printed recursively.");
  39.    return EXIT_SUCCESS;
  40. }
  41. --
  42. Robert B. Clark <rclark@iquest.net>
  43. "Always listen to experts.  They'll tell you what can't be done, and why.  Then do it." --RAH
  44.